package com.kyzh.core.pager.me.settings

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.gushenge.core.beans.IndexTop
import com.gushenge.core.dao.MMKVConsts
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.databinding.ActivityStandardSettingsBinding
import com.kyzh.core.databinding.ItemSettingsSwitchBinding
import com.kyzh.core.uis.alert
import com.kyzh.core.utils.Config
import com.kyzh.core.utils.FileUtils
import com.kyzh.core.utils.setLanguage
import info.muge.appshare.adapters.LanguageAdapter
import kotlinx.android.synthetic.main.activity_phonelogin.*
import kotlinx.android.synthetic.main.activity_standard_settings.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jetbrains.anko.sdk27.coroutines.onCheckedChange
import org.jetbrains.anko.toast
import kotlin.system.exitProcess

class StandardSettingsActivity : BaseActivity() {
    private lateinit var binding:ActivityStandardSettingsBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this,R.layout.activity_standard_settings)

        initView()
    }

    private fun initView() {
        binding.apply {
            vReceiveNotice.setInfo(getString(R.string.receiveEventNotice),MMKVConsts.receiveNotice)
            vReceiveNotice.sw1.setOnCheckedChangeListener { buttonView, isChecked ->
                MMKVConsts.receiveNotice = isChecked
            }
            swGeXing.onCheckedChange { buttonView, isChecked ->
                if(!isChecked){
                    toast("您已关闭个性化推荐")
                }else{
                    toast("您已开启个性化推荐")
                }
            }
            vNotificationBarNotice.setInfo(getString(R.string.chatNoticeNotifyBar),MMKVConsts.notificationBarNotice)
            vNotificationBarNotice.sw1.setOnCheckedChangeListener { buttonView, isChecked ->
                MMKVConsts.notificationBarNotice = isChecked
            }
            vPointNotice.setInfo(getString(R.string.getPointNotice),MMKVConsts.pointNotice)
            vPointNotice.sw1.setOnCheckedChangeListener { buttonView, isChecked ->
                MMKVConsts.pointNotice = isChecked
            }
            vAutoUpdate.setInfo(getString(R.string.autoCheckUpdate),MMKVConsts.autoUpdate)
            vAutoUpdate.sw1.setOnCheckedChangeListener { buttonView, isChecked ->
                MMKVConsts.autoUpdate = isChecked
            }
            vSaveTraffic.setInfo(getString(R.string.saveTraffic),MMKVConsts.saveTraffic,getString(R.string.saveTraffic1))
            vSaveTraffic.sw1.setOnCheckedChangeListener { buttonView, isChecked ->
                MMKVConsts.saveTraffic = isChecked
            }
            vClearCache.apply {
                text = getString(R.string.cleanCache)
                desc = getString(R.string.cleanCache1)
                desc2 = FileUtils.formatSize(getExternalFilesDir("")?.path?:"")
                root.setOnClickListener {
//                    OkDownload.getInstance().removeAll(true)
                    FileUtils.deleteFileExceptFolder(getExternalFilesDir("")!!.path)
                    desc2 = FileUtils.formatSize(getExternalFilesDir("")!!.path)
                }
            }
            Gson().fromJson<ArrayList<IndexTop>>(MMKVConsts.languageConfig,object : TypeToken<ArrayList<IndexTop>>(){
            }.type) .apply {
                    val spTypeAdapter = LanguageAdapter(this@StandardSettingsActivity, android.R.layout.simple_spinner_dropdown_item,this)
                spLanguage.setPopupBackgroundDrawable(ContextCompat.getDrawable(this@StandardSettingsActivity,R.drawable.bg_6dp_white))
                spLanguage.adapter = spTypeAdapter
                this.forEachIndexed { index, indexTop ->
                    if (indexTop.type == MMKVConsts.language.toInt()){
                        spLanguage.setSelection(index)
                    }
                }
                spLanguage.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
                    override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                        if (this@apply[p2].type.toString() != MMKVConsts.language){
                            alert(getString(R.string.restartAppNow),null,getString(R.string.sure),getString(R.string.cancel),{
                                MMKVConsts.language = this@apply[p2].type.toString()
                                Config.localLanguage = this@apply[p2].type.toString()
                                setLanguage(this@apply[p2].type.toString())
                                GlobalScope.launch(Dispatchers.Main) {
                                    delay(500)
                                    exitProcess(0)
                                }
                            },{})
                        }
                    }

                    override fun onNothingSelected(p0: AdapterView<*>?) {
                    }

                }
            }

        }
    }
    private fun ItemSettingsSwitchBinding.setInfo(title:String,state:Boolean,desc:String=""){
        this.text = title
        this.sw1.isChecked = state
        this.desc = desc
    }
}